package cn.com.tuns.assess.qsb.frame.widget;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

public class BaseListAdapter<T> extends BaseAdapter {
	private List<T> datas;
	private Context context;
	private OnGetView<T> onGetView;

	private int count;

	public BaseListAdapter(Context context) {
		this.context = context;
	}

	@Override
	public int getCount() {
		if (count > 0)
			return count;
		if (datas == null)
			return 0;
		return datas.size();
	}

	@Override
	public Object getItem(int i) {
		if (datas == null)
			return null;
		return datas.get(i);
	}

	@Override
	public long getItemId(int i) {
		return 0;
	}

	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		if (datas != null)
			return onGetView.onGetView(context, i, datas.get(i), view);
		else
			return onGetView.onGetView(context, i, null, view);
	}

	public static interface OnGetView<T> {
		public View onGetView(Context context, int i, T obj, View view);
	}

	public void setDatas(List<T> datas) {
		this.datas = datas;
		notifyDataSetChanged();
	}

	public void setCount(int count) {
		this.count = count;
	}

	public void setOnGetView(OnGetView<T> onGetView) {
		this.onGetView = onGetView;
	}
}
